#--coding:utf-8--
# @Time    : 2022/4/28/028 23:10
# @Author  : hakold
# @File    : register_model.py
# @PROJECT : chatroom_game

import json
import pymongo

def mongo_connect():
    #myclient = pymongo.MongoClient('mongodb+srv://hakold:52Beebee@cluster0.gy1rb.mongodb.net/xiuxian?retryWrites=true&w=majority/')
    myclient = pymongo.MongoClient('mongodb://127.0.0.1:27017/')
    #dblist = myclient.list_database_names()
    # dblist = myclient.database_names() 
    mydb = myclient["qt_game"] #创建数据库
    #collist = mydb.list_collection_names()
    mycol = mydb["player"] #集合
    return mycol


def register(data_dict):  # 注册逻辑
    account = data_dict["account"].strip()
    password = data_dict["password"].strip()
    nickname = data_dict["nickname"].strip()
    data = {}
    code = ""
    msg = ""
    print(account, password, nickname)
    if account and password and nickname: #account、password、nickname均不为空，才走注册验证逻辑
        mycol = mongo_connect()
        if register_check_account(mycol, account) and register_check_nickname(mycol, nickname):
            # 可以注册
            player_data = init_player()
            player = {
                "account": account,
                "password": password,
                "nickname": nickname,
                "level" : player_data['level'],
                "hp" : player_data['hp'],
                "max_hp": player_data['max_hp'],
                "ad" : player_data['ad'],
                "ad_def" : player_data['ad_def'],
                "ap_def" : player_data['ap_def'],
                "ap" : player_data['ap'],
                "exp" : player_data['exp'],
                "money": player_data['money'],
                "diamond": player_data['diamond'],
                "mission": player_data['mission'],
                "bag": player_data['bag'],
                "equip": player_data['equip'],
                "home": player_data['home']
            }
            if register_save(mycol, player):
                data['data'] = player
                msg = "注册成功"
                code = "201"
            else:
                msg = "注册系统错误"
                code = "505"

    elif not account:
        code = "502"
        msg = "注册账号不能为空"

    elif not password:
        code = "503"
        msg = "注册密码不能为空"

    elif not nickname:
        code = "504"
        msg = "注册昵称不能为空"

    data["type"] = "register"
    data["code"] = code
    data["msg"] = msg
    data = json.dumps(data)
    print(data)
    return data
 
def register_check_account(mycol, account):
    check_query = { "account" : account }
    check_doc = mycol.find(check_query)
    check_list = []
    for x in check_doc:
        check_list.append(x)
    if len(check_list) > 0:
        code = "505"
        msg = "用户名已存在"
        return False
    else:
        return True

def register_check_nickname(mycol, nickname):
    check_query = { "nickname" : nickname }
    check_doc = mycol.find(check_query)
    check_list = []
    for x in check_doc:
        check_list.append(x)
    if len(check_list) > 0:
        code = "505"
        msg = "该角色名字已存在"
        return False
    else:
        return True

def register_save(mycol, player_data):
    try:
        save_command = mycol.insert_one(player_data)
        print(player_data)
        del player_data["_id"]
        return [True,player_data]
    except:
        return [False]
    
               
def init_player():
    hp = 100 # 生命值
    ad = 10 # 攻击
    ad_defend = 10 # 防御
    ap_defend = 10 # 法术防御
    ap = 0 # 灵力
    exp = 0 # 修为
    level = 1 # 境界
    money = 1000 # 灵石
    diamond = 0 # 钻石
    # 任务
    mission = {
        "now_mission":[
            {
                "id": 1001,
                "status": 0,
                "type": 1
            }
        ],
        "finish_mission":[]
    }
    # 背包
    bag = {
        '10014': 5,
        '10006': 5
    }
    # 装备
    equip = {
        '1': [
            {
                "id": 1001,
                "status": 1,
                "attr1": 1,
                "attr2": 2
            },
            {
                "id": 1001,
                "status": 0,
                "attr1": 1,
                "attr2": 2
            },
        ],
        '2': [
            {
                "id": 2001,
                "status": 0,
                "attr1": 2,
                "attr2": 5
            },
            {
                "id": 2002,
                "status": 0,
                "attr1": 2,
                "attr2": 8
            },
        ],
        '3': [
            {
                "id": 3001,
                "status": 0,
                "attr1": 50,
                "attr2": 50
            }
        ],
        '4': [
            {
                "id": 4001,
                "status": 0,
                "attr1": 2,
                "attr2": 2
            },
        ],
        '5': [
            
        ],
        '6': [
            {
                "id": 6001,
                "status": 0,
                "attr1": 15,
                "attr2": 10
            },
        ],
    }
    # 洞府
    home = {
        "last_exercise_time": "",
        "gather_exp_field": {
            "level": 1,
        }
    }
    player_data = {
        "level" : level,
        "hp" : hp,
        "max_hp": hp,
        "ad" : ad,
        "ap" : ap,
        "ad_def" : ad_defend,
        "ap_def" : ap_defend,
        "exp" : exp,
        "money": money,
        "diamond": diamond,
        "mission": mission,
        "bag" : bag,
        "equip": equip,
        "home": home
    }
    return player_data